package com.lagou.web.servlet;

import com.lagou.pojo.Course;
import com.lagou.service.CourseService;
import com.lagou.service.impl.CourseServiceImpl;
import com.lagou.utils.DateUtils;
import com.lagou.utils.UUIDUtils;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.io.IOUtils;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/courseSalesInfo")
public class CourseSalesInfoServlet extends HttpServlet {
    /**
     * 保存课程营销信息
     *
     * @param req  an {@link HttpServletRequest} object that
     *             contains the request the client has made
     *             of the servlet
     * @param resp an {@link HttpServletResponse} object that
     *             contains the response the servlet sends
     *             to the client
     * @throws ServletException
     * @throws IOException
     */
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        try {
            Course course = new Course();
            Map<String, Object> map = new HashMap<>();
            DiskFileItemFactory factory = new DiskFileItemFactory();
            ServletFileUpload fileUpload = new ServletFileUpload(factory);
            List<FileItem> list = fileUpload.parseRequest(req);
            for (FileItem item : list) {
                boolean formField = item.isFormField();
                if (formField) {
                    String fieldName = item.getFieldName();
                    String value = item.getString("UTF-8");
                    map.put(fieldName, value);
                } else {
                    String fileName = item.getName();
                    String newFileName = UUIDUtils.getUUID() + "_" + fileName;
                    InputStream in = item.getInputStream();
                    String realPath = this.getServletContext().getRealPath("");
                    String uploadPath = realPath + "upload" + File.separator;
                    File file = new File(uploadPath);
                    if (!file.exists()) {
                        file.mkdir();
                    }
                    String fullPath = uploadPath + newFileName;
                    FileOutputStream out = new FileOutputStream(fullPath);
                    IOUtils.copy(in, out);
                    out.close();
                    in.close();
                    map.put("course_img_url", fullPath);
                }
            }
            BeanUtils.populate(course, map);
            String dateFormat = DateUtils.getDateFormat();
            CourseService cs = new CourseServiceImpl();
            String result = "";
            if (map.get("id") != null) {
                course.setUpdate_time(dateFormat);
                result = cs.updateCourseSalesInfo(course);
            } else {
                course.setCreate_time(dateFormat);
                course.setUpdate_time(dateFormat);
                course.setStatus(1);
                result = cs.saveCourseSalesInfo(course);
            }
            resp.getWriter().print(result);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
